package com.ttl.bc.ap.invoice.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ap.invoice.bo.BCAPInvoiceBO;
import com.ttl.common.util.CommonUtils;

public class SelectPRAction extends DispatchAction {
	
	public ActionForward blank_po(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");				
		@SuppressWarnings("unused")
		String preDocNo  = (String)request.getSession().getAttribute("preDocNo");				
						
//		String docNo     = CommonUtils.getMaxBCPurchaseOrderDocNo();		
		request.getSession().setAttribute("userName", user);
//		request.getSession().setAttribute("docNo", docNo);		
						
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :SelectPRAction #mode :blank_po");
		
		return mapping.findForward("blank_po");
	}	
	
	//ค้นหา PR=Purchase Request
	public ActionForward prFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCAPInvoiceAction #mode :moreItemsShow ");

//		DynaActionForm pr = (DynaActionForm)form;		
//		String	itemFind = pr.get("itemFind").toString();
//		itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");		//แปลงให้ค้นหาภาษาไทยได้		

		PRFindForm pr = (PRFindForm)form;
		String itemFind = "";
		if(pr.getItemFind()!=null) {
			itemFind = pr.getItemFind(); 
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");		//แปลงให้ค้นหาภาษาไทยได้
		}
		
		request.setAttribute("itemFind", itemFind);		
		
		String hql = "FROM BCReqConfirmBean WHERE docNo like '%"+itemFind+"%' ORDER BY docNo";
		new BCAPInvoiceBO().setPage(request, hql);
		
		return mapping.findForward("success_prFind");
	}	
	
	@SuppressWarnings("unchecked")
	public ActionForward findPOSelected(ActionMapping mapping, ActionForm form, 
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCAPInvoiceAction #mode :findPOSelected");
				
		BCAPInvoiceBO bo = new BCAPInvoiceBO();
		ArrayList results = (ArrayList)bo.find(form, request);		
		
//		request.getSession().setAttribute("userName", user);		
		request.setAttribute("resultList_prpo", results);
				
		request.setAttribute("countId", results.size());
		request.setAttribute("taxRate", "7.00");
		request.setAttribute("exchangeRate", "1.00");
		
		
		return mapping.findForward("blank_showData");
	}		
}
